package pe.com.certicom.scv.controller;

import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import pe.com.certicom.scv.domain.Pandero;
import pe.com.certicom.scv.domain.User;
import pe.com.certicom.scv.service.ScvAdminService;
import pe.com.certicom.scv.validator.UsuarioValidator;


@Controller
public class InicioController {
    
    @Autowired
    private ScvAdminService ScvAdminService;
    
    @RequestMapping(value ="/inicio", method = RequestMethod.GET)
    public String iniciarApp(Model model,HttpSession session) throws Exception {
        model.addAttribute(new User());
    	session.setAttribute("UsuarioSesion", new User());
        return "/login/login";
    }
 
    @RequestMapping(value = "/login/procesar", method = RequestMethod.POST)
    public String processLoginSubmit(Model modelo, HttpSession session, @ModelAttribute User usuario, 
    	BindingResult result, SessionStatus status) {
    	User usuariodb = ScvAdminService.getUser(usuario.getName(),usuario.getPass());
		new UsuarioValidator().validarLogin(usuario, usuariodb, result);
		if (result.hasErrors())return "login/login";
		else {
			session.setAttribute("UsuarioSesion", usuariodb);
			modelo.addAttribute("Pandero", new Pandero());
			status.setComplete();
			return "common/bienvenida";
		}
	}
    
    @RequestMapping(value = "/login/logout", method = RequestMethod.GET)
    public String cerrarSession(Model model,HttpSession session) throws Exception{
    	return iniciarApp(model,session);
    	
    }
    
}







